var vue = null;
$(function() {
    init_vue();
});

function init_vue() {
    vue = new Vue({
        el : "#myapp",
        data : {
            startRow : 0,
            endRow : 0,
            total : 0,
            firstPage : 0,
            lastPage : 0,
            pageNum : 1,
            pageSize : 13,
            list : null,
            setting : null,
            navigatepageNums : null,
            agent_id:0,
            organization_id:'',
            organization_names:'',
            data_cycle:'',
            nick_name:'',
            mobile_phone:'',
            play_type:0,
            name_arr:null,
            value_arr:null,
            sum:null,
			RefreshTime:0,
            loadType:1,
            /*order_num:0,
            order_real_price:0,
            order_fee_money:0,
            order_order_price:0,*/

        },
        events : {
            'refreshUserList':function () {
                this.pageNum = 1;
                this.getData();
            },
            'refreshUserInfo': function (t) {
                for (var i=0; i<this.list.length; i++) {
                    if (this.list[i].id == t.user.id) {
                        this.list[i].real_name = t.user.real_name;
                        this.list[i].mobile_phone = t.user.mobile_phone;
                    }
                }
            },
            "selectOrg": function (t) {
                this.organization_id = t.organization_ids;
                this.organization_names = t.organization_names;
            }
        },
        methods : {
            init : function() {
                close_layuiLayerLoad();
                this.initControl();
                this.getData();
            },
            initControl:function () {
                var t = this;
                $('#data_cycle').daterangepicker({
                    type: 2,
                    // drops: 'up',
                    // startDate: moment().subtract('year', 1),
                    startDate: d,
                    endDate: moment().add('days', 1),
                    autoApply:false,
                    autoUpdateInput:true,
                    showDropdowns : true,
                    ranges : {
                        '今日': [moment().startOf('day'), moment().add('days', 1)],
                        '昨日': [moment().subtract('days', 1),  moment()],
                        '最近7日': [moment().subtract('days', 6), moment().add('days', 1)],
                        '最近30日': [moment().subtract('days', 29), moment().add('days', 1)]
                    },
                    opens : 'right', //日期选择框的弹出位置
                    separator : ' to ',
                    locale : {
                        separator:'/',
                        format : 'YYYY-MM-DD', //控件中from和to 显示的日期格式
                        applyLabel : '确定',
                        cancelLabel : '取消',
                        fromLabel : '起始时间',
                        toLabel : '结束时间',
                        customRangeLabel : '自定义',
                        daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
                        monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
                            '七月', '八月', '九月', '十月', '十一月', '十二月' ],
                        firstDay : 1
                    }
                }, function(start, end, label) {
                    //格式化日期显示框
                    t.data_cycle = start.format('YYYY-MM-DD') + '/' + end.format('YYYY-MM-DD');
                });
            },
            showPage : function(page) {
                // alert(page);
				this.loadType = 2;
                this.pageNum = page;
                this.getData();
            },
            getData : function() {
                showLoad();
                var t = this;
                postMsg(root+"/order/tolist", {
                    page_num : t.pageNum,
                    page_size : t.pageSize,
					loadType: t.loadType,
                    type:1,
                    data_cycle: t.data_cycle,
                    nick_name: t.nick_name,
                    mobile_phone: t.mobile_phone,
                    agent_id:t.agent_id,
                    organization_id:t.organization_id,
                    play_type:t.play_type
                }, function(i) {
					if (i.RefreshTime >= 0) {
                        t.RefreshTime = i.RefreshTime;
                        // layer.alert(t.RefreshTime+1);
                        // void setTimeout(this.refreshDate, 1e3);
                        t.refreshDate();
                    } else {
						t.list = null;
						t.list = i.list;
						t.name_arr = i.name_arr;
						t.value_arr = i.value_arr;
						t.startRow = i.startRow;
						t.endRow = i.endRow;
						t.total = i.total;
						t.firstPage = i.firstPage;
						t.lastPage = i.lastPage;
						t.navigatepageNums = i.navigatepageNums;

						t.sum = i.sum;

						// t.order_num = i.order_num;
						// t.order_real_price = i.order_real_price;

						if (t.list != null) {
							for (var li=0; li<t.list.length; li++) {
								t.list[li].order_fee = parseFloat((t.list[li].real_price * t.list[li].mun) * (t.list[li].order_fee/100)).toFixed(2)
							}
						}
					}

                    // t.order_fee_money=i.order_fee_money;
                    // t.order_order_price=i.order_order_price;

                    // t.total_balance = i.total_balance;
                    hideLoad();
                });
            },
			refreshDate: function () {
                var t = this;
                var timer;
                layer.open({
                    type:1,
                    title: ['查询动作太快','color:red'],
                    area: ['340px','156px'], anim: 6,
                    content: '<div style="padding:19px;font-size:15pt;color:blue;">查询动作太快了，先休息一下吧！</div>',
                    success: function(a,b){
                        var i = t.RefreshTime+1;
                        var fn = function() {
                            layer.title((i+1)+' 秒后自动进行刷新数据操作',b);
                            if(!i) {layer.close(b);t.getData()} i--;
                        };
                        timer = setInterval(fn, 1000); fn();
                    },
                    end: function(){clearInterval(timer);}
                });
                //clearInterval(checkLogin);	//# 去除后，似乎不能再次激活。。。

            },
            query : function () {
                this.pageNum = 1;
                this.getData();
            },
            reset_query : function () {
                agent_id_select.val(null).trigger("change");
                this.agent_id=0;
                this.organization_id='';
                this.organization_names='';
                this.data_cycle=d+'/'+moment().format('YYYY-MM-DD');
                this.nick_name='';
                this.mobile_phone='';
                this.play_type = 0;
                this.getData();
            },
            openTreeOrganizationWin: function () {
                this.$broadcast('openTreeOrganizationWin');
            },
            exportexcel:function () {
                var t = this;
                t.pageNum = 1;
                t.getData();

                /*data_cycle: t.data_cycle,
                nick_name: t.nick_name,
                mobile_phone: t.mobile_phone,
                agent_id:t.agent_id,
                organization_id:t.organization_id*/

                var url = root + '/order/exportexcel?data_cycle='+t.data_cycle+"&nick_name="+t.nick_name+"&mobile_phone="+t.mobile_phone+"&agent_id="+t.agent_id+"&organization_id="+t.organization_id+"&play_type="+t.play_type;

                layer.open({
                    type: 2,
                    title: false,
                    closeBtn: 0, //不显示关闭按钮
                    shade: [0],
                    area: ['0px', '0px'],
                    offset: 'rb', //右下角弹出
                    time: 2000, //2秒后自动关闭
                    anim: 2,
                    content: [url, 'no'] //iframe的url，no代表不显示滚动条
                });
                // window.location.href = root + '/user/exportexcel';
            },
            gotoInfoPage : function (id) {
                window.location.href = root + '#user/info/id/' + id;
            }
        }
    });
    vue.init();
}

Vue.filter('jgvalue',
    function(index, item) {
        var key  = "p_"+index+"name";
        return eval("item."+key);
    }
);